import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

const lazyLoad = (str) => {
  return (resolve) => {
    import('@/' + str).then((module) => {
      resolve(module)
    }).catch()
  }
}

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      component: lazyLoad('views/Index')
    },
    {
      path: '/login',
      name: 'login',
      component: lazyLoad('views/Login')
    },
    {
      path: '/reset',
      name: 'reset',
      component: lazyLoad('views/Reset')
    },
    {
      path: '/FAQ',
      name: 'FAQ',
      component: lazyLoad('views/FAQ')
    },
    {
      path: '/forget',
      name: 'forget',
      component: lazyLoad('views/Forget')
    },
    {
      path: '/personal',
      name: 'personal',
      component: lazyLoad('views/Personal'),
      beforeEnter: (to, from, next) => {
        if (store.state.user.token === '') {
          next({
            path: '/index'
          })
        } else {
          console.log(store.state.user.token)
          next()
        }
      }
    },
    {
      path: '/invite',
      name: 'invite',
      component: lazyLoad('views/Invite'),
      beforeEnter: (to, from, next) => {
        if (store.state.user.token === '') {
          next({
            path: '/index'
          })
        } else {
          console.log(store.state.user.token)
          next()
        }
      }
    },
    {
      path: '/cash',
      name: 'cash',
      component: lazyLoad('views/Cash'),
      beforeEnter: (to, from, next) => {
        if (store.state.user.token === '') {
          next({
            path: '/index'
          })
        } else {
          next()
        }
      }
    },
    {
      path: '/poster',
      name: 'poster',
      component: lazyLoad('views/Poster')
    },
    {
      path: '*',
      redirect: '/index'
    }
  ]
})

// import { Loading } from 'element-ui'
// let fullscreenLoading = null
// router.beforeEach((to, from, next) => {
//   fullscreenLoading = Loading.service({ fullscreen: true })
//   next()
// })

// router.afterEach((to, from) => {
//   // Loading.service({ fullscreen: false })
//   fullscreenLoading.close()
// })

export default router
